<template>
    <div class="djRadio">
        <!-- 声音主播 -->
        <h2 class="head2">声音主播</h2>
        <div class="djRadio_list">
            <ul>
                <li v-for="(item, index) in djRadios" :key="index">
                    <img class="pic" :src="item.picUrl" alt="" @click="Go(item)">
                    <h3 class="title ellipsis" :title="item.name" @click="Go(item)" v-html="highlight(item.name)"></h3>
                    <div class="user ellipsis">
                        <span>by</span>
                        <a :title="item.dj.nickname" @click="$router.push(`/user/${item.dj.userId}`)">{{ item.dj.nickname }}</a>
                        <i :class="{'male':item.dj.gender==1}"></i>
                        <i :class="{'female':item.dj.gender==2}"></i>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import { DjRadioSearchApi } from "@/request/api";
export default {
    data () {
        return {
            djRadios: [],
        }
    },
    computed: {
        keyword() {
            return this.$route.params.keyword;
        }
    },
    watch: {
        keyword: {
            handler(newValue, oldValue){
                if(newValue !== oldValue){
                    DjRadioSearchApi(newValue).then((res) => {
                        // console.log(res);
                        this.djRadios = res.data.result.djRadios;
                    });
                }
            },
            immediate: true,
        }
    },
    // created() {
    //     DjRadioSearchApi(this.keyword).then((res) => {
    //         // console.log(res);
    //         this.djRadios = res.data.result.djRadios;
    //     });
    // },
    methods: {
        highlight(str) {
            // 正则表达式//中间的字符只是字符串
            // 如果需要动态创建一个正则表达式
            // new RegExp(str, rules) str是正则字符串，rules是匹配模式写在字符串中
            const reg = new RegExp(this.keyword, 'gi')
            const highStr = str.replace(reg, `<span style="color: #0c73c2;">${this.keyword}</span>`)
            return highStr
        },
        Go(item) {
            this.$router.push(`/djradio/${item.id}`);
        }
    }
}
</script>
 
<style lang = "less" scoped>
@import "../../assets/base.less";
.djRadio {
    .head2 {
        font-size: 14px;
        color: #333;
        padding: 10px 0;
        border-bottom: 1px solid #dadada;
        line-height: 17px;
        font-weight: bold;
    }
    .djRadio_list {
        ul {
            flex-wrap: wrap;
            display: flex;
            justify-content: flex-start !important;
            margin-top: 20px;
        }
        li {
            width: 150px;
            height: 200px;
            margin: 0 42px 40px 0;
            &:nth-of-type(5n) {
                margin-right: 0;
            }
            .pic {
                cursor: pointer;
                width: 150px;
                height: 150px;
            }
            .title {
                margin: 10px 0 6px;
                line-height: 16px;
                font-size: 14px;
                color: #333;
                &:hover {
                    cursor: pointer;
                    text-decoration: underline;
                }
            }
            .user {
                display: flex;
                align-items: center;
                width: 150px;
                line-height: 18px;
                span {
                    font-size: 12px;
                    color: #999;
                    margin: 0 5px 0 0;
                }
                a {
                    font-size: 12px;
                    color: #333;
                    &:hover {
                        cursor: pointer;
                        text-decoration: underline;
                    }
                }
                .male {
                    margin: 0 5px;
                    width: 14px;
                    height: 14px;
                    display: inline-block;
                    background: url("@/assets/images/public/icon.png") no-repeat -70px -20px;
                }
                .female {
                    margin: 0 5px;
                    width: 14px;
                    height: 14px;
                    display: inline-block;
                    background: url("@/assets/images/public/icon.png") no-repeat -70px 0;
                }
            }
        }
    }
}
</style>